<template>
  <div class="add-container">
    <div class="add-top">
      <el-page-header @back="goBack" :content="gobacktitle"></el-page-header>
    </div>
    <div class="add-main">
      <div class="add-main-top">
        <div class="add-main-tital">
          <p>入库任务单</p>
        </div>
        <div>
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            label-position="left"
            label-width="0.8rem"
            class="demo-ruleForm"
          >
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="入库单号">
                  <el-input
                    v-model="ruleForm.rukudh"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="起始点">
                  <el-input v-model="ruleForm.qishid"></el-input>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="6">
                <el-form-item label="终结点">
                  <el-input v-model="ruleForm.zhongjied"></el-input>
                </el-form-item>
              </el-col> -->
              <el-col :span="6">
                <el-form-item label="货位号">
                  <el-select
                    v-model="ruleForm.huoweih"
                    @change="getHuowei"
                    clearable
                    filterable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, index) in huoweilist"
                      :key="index"
                      :label="item.huoweibh"
                      :value="item.huoweibh"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="托盘号">
                  <el-select
                    v-model="ruleForm.tuopanh"
                    @change="getTuoPan"
                    clearable
                    filterable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, index) in tuopanlist"
                      :key="index"
                      :label="item.tuopanbh"
                      :value="item.tuopanbh"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="入库状态">
                  <el-select v-model="ruleForm.zhuangtai" placeholder="请选择">
                    <el-option
                      v-for="(item, index) in $util.getSjzt()"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <div class="add-main-list">
        <div class="table-list" id="table-list">
          <ux-grid
            border
            show-overflow
            ref="plxTable"
            keep-source
            :height="tabheight"
            :highlightCurrentRow="false"
            widthResize
            :rules="ruleslist"
            @selection-change="selectChange"
            :edit-config="{ trigger: 'click', mode: 'row' }"
          >
            <!-- <ux-table-column type="checkbox" width="50" /> -->
            <ux-table-column
              type="index"
              title="序号"
              width="50"
            ></ux-table-column>
            <!-- <ux-table-column field="wuzibm" title="物资代码"></ux-table-column> -->

            <ux-table-column
              field="waiguamc"
              title="挂架名称"
            ></ux-table-column>
            <ux-table-column
              field="waiguamc"
              title="唯一编码"
            ></ux-table-column>
            <ux-table-column field="leixing" title="型别"></ux-table-column>

            <ux-table-column field="xianchuwz" title="现处位置">
              <template v-slot="scope">
                {{ $util.getguajiawz(scope.row.xianchuwz) }}
              </template>
            </ux-table-column>
            <ux-table-column field="guajiazt" title="挂架状态">
              <template v-slot="scope">
                {{ $util.getguajiazt(scope.row.guajiazt) }}
              </template>
            </ux-table-column>
          </ux-grid>
          <div class="submit-button">
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submitForm('ruleForm')"
            >
              确认
            </el-button>
            <el-button type="info" icon="el-icon-delete" @click="goBack">
              取消
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import {
    insertrukurwd,
    queryKufangList,
    updaterukurwd,
  } from '@/api/warehousing'
  import { queryguajiaList, queryguajiaListIDS } from '@/api/kunei'
  import { queryHuoWeiList, queryTuoPanListAll } from '@/api/cangkujichu'
  export default {
    name: 'Page2Add',
    props: {
      gobacktitle: {
        type: String,
        default: '新增',
        required: true,
      },
      datarow: {
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        show: true,
        insertData: {},
        ruleForm: {},
        rules: {
          rukudh: [
            { required: true, message: '请输入入库单号', trigger: 'blur' },
          ],
          rukulx: [
            { required: true, message: '请选择入库类型', trigger: 'blur' },
          ],
        },
        ruleslist: {
          wuzimc: [
            { required: true, message: '请输入物资名称', trigger: 'blur' },
          ],
          jihuarksl: [
            { required: true, message: '请输入数量', trigger: 'blur' },
          ],
          chubeidj: [
            { required: true, message: '请输入储备等级', trigger: 'blur' },
          ],
          danjia: [{ required: true, message: '请输入单价', trigger: 'blur' }],
        },
        tabheight: 500,
        importShow: false,
        KufangList: [],
        cbdjList: [],
        Wzzylist: [],
        guajialist: [],
        huoweilist: [],
        tuopanlist: [],
      }
    },
    created() {
      this.getGuajiaList()
    },
    async mounted() {
      let height = document.getElementById('table-list').offsetHeight
      this.tabheight = height - 94
      let arr = []
      this.ruleForm = Object.assign(
        {
          zhuangtai:
            this.gobacktitle == '新增' ? this.$util.getSjzt()[0].id : null,
        },
        this.datarow
      )

      // this.$nextTick(() => {
      //   this.$refs.plxTable.reloadData(this.ruleForm.rukudmxList)
      // })
    },
    methods: {
      getHuowei(e) {
        let item = this.huoweilist.find((item) => {
          return item.huoweibh == e
        })
        this.ruleForm.huoweihid = item.id
      },
      getTuoPan(e) {
        let item = this.tuopanlist.find((item) => {
          return item.tuopanbh == e
        })
        this.ruleForm.tuopanhid = item.id
      },
      async getGuajiaList() {
        const res = await queryguajiaListIDS({ ids: this.datarow.guajiamid })
        const res2 = await queryHuoWeiList()
        const res3 = await queryTuoPanListAll()
        this.guajialist = res.data
        this.huoweilist = res2.data
        this.tuopanlist = res3.data
        this.$refs.plxTable.reloadData(res.data)
        // if (this.gobacktitle == '新增') return
        // this.$nextTick(() => {
        //   this.$refs.plxTable.toggleRowSelection([
        //     {
        //       row: this.guajialist.find((item) => {
        //         return item.id == this.datarow.guajiamid
        //       }),
        //       selected: true,
        //     },
        //   ])
        // })
      },
      // 编辑时，计划入库数量限制不得小于已验收数量
      changeNum(scope) {
        if (
          scope.row.yiyanshousl &&
          scope.row.yiyanshousl > 0 &&
          scope.row.jihuarksl < scope.row.yiyanshousl
        ) {
          this.$message({
            message: `当前编辑入库数量不得少于已验收数量 ${scope.row.yiyanshousl}`,
            type: 'warning',
          })
          scope.row.jihuarksl = scope.row.yiyanshousl
        }
      },
      dataUpdate() {
        this.$forceUpdate()
      },
      goBack() {
        this.$parent.addshow = false
        this.$parent.fetchData()
      },

      DialogOpenWuzi(row) {
        this.$refs['wuzi-dialog'].wuzizyid = this.ruleForm.wuzizy
        this.$refs['wuzi-dialog'].showEdit(row)
        this.$refs['wuzi-dialog'].getData()
      },
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            if (this.$refs.plxTable.getCheckboxRecords().length == 0) {
              this.$message({
                message: '请选择挂架',
                type: 'warning',
              })
              return false
            }
            if (this.$refs.plxTable.getCheckboxRecords().length > 1) {
              this.$message({
                message: '请勾选单独一个挂架',
                type: 'warning',
              })
              return false
            }
            let item = this.$refs.plxTable.getCheckboxRecords()
            this.ruleForm.guajiam = item[0].guadanghm
            this.ruleForm.guajiamid = item[0].id

            if (this.gobacktitle == '新增') {
              const msg = await insertrukurwd(this.ruleForm)
              this.$message({
                message: msg.msg,
                type: 'success',
              })
            } else {
              const msg = await updaterukurwd(this.ruleForm)
              this.$message({
                message: msg.msg,
                type: 'success',
              })
            }
            this.resetForm() //重置表单
            this.$parent.resetlist()
          } else {
            return false
          }
        })
      },

      resetForm(formName) {
        this.$parent.addshow = false
      },
      selectChange(e) {},
      // 添加插入行
      async insertEvent(row) {
        // let record = {
        //   wuziid: this.insertData[0].id,
        //   wuzimc: this.insertData[0].wuzimc,
        //   wuzibm: this.insertData[0].wuzibm,

        //   guigexh: this.insertData[0].guigexh,
        //   jiliangdw: this.insertData[0].jiliangdw,
        // }
        let record = this.insertData
        // 加入行, 表示加入行的row对象，row为空则插入第一行，为-1插入最后一行
        let { row: newRow } = await this.$refs.plxTable.insertRow(record)
        // 激活单元格编辑
        await this.$refs.plxTable.setActiveRow(newRow)
      },
      delwzDetail() {
        this.$refs.plxTable.remove(this.$refs.plxTable.getCheckboxRecords())
      },
      uploadSuccess(res) {
        this.ruleForm.rukudmxList = res.rukudbdmxDto
        this.importShow = false
        this.$nextTick(() => {
          this.$refs.plxTable.reloadData(this.ruleForm.rukudmxList)
        })
      },
      oninput(num) {
        return num.replace(/\D/g, '')
      },
      KufangListOne(id) {
        if (id && this.cbdjList.length > 0) {
          let datarow = this.cbdjList.filter((item) => item.id == id)
          return datarow[0].mingcheng
        }
        return null
      },
      fetchData(data) {
        this.insertData = data
        this.insertData.forEach((item) => {
          item.wuziid = item.id
          item.danjia = item.cankaojg //里面没有单价，但是有参与价格
        })
        this.insertEvent()
      },
    },
  }
</script>
<style lang="scss" scoped>
  .add-container {
    position: fixed;
    left: 0;
    top: 0.75rem;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background-color: #f6f8f9;
    padding: 0 0.2rem 1rem 0.2rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .add-top {
      padding: 0.1rem 0;

      ::v-deep .el-page-header__title {
        font-size: 0.16rem !important;
      }
    }

    .add-main {
      flex: 1;
      font-weight: bold;
      display: flex;
      flex-direction: column;
      overflow: auto;

      .add-main-top {
        padding: 0.2rem;
        background: #fff;
        border-radius: 0.05rem;

        .add-main-tital {
          font-size: 0.18rem;
          border-left: 0.1rem solid rgb(121, 189, 255);
          padding-left: 0.1rem;

          p {
            margin: 0;
          }
        }

        .search-gropu-item {
          margin-top: 0.2rem;
          width: 90%;
          display: flex;
          // label {
          //   white-space: nowrap;
          //   line-height: 32px;
          //   margin-right: 10px;
          // }
        }

        .el-select {
          width: 100%;
        }
      }

      .add-main-list {
        flex: 1;
        margin-top: 20px;
        background: #fff;
        padding: 20px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;

        .list-button-group {
          display: flex;
          flex-direction: row;
          padding-bottom: 20px;

          .button-item {
            padding: 0px 10px;
          }

          .button-item:first-child,
          &:last-child {
            padding: 0px 10px 0 0;
          }
        }

        .table-list {
          flex: 1;
        }
      }

      .el-button {
        font-size: 0.14rem !important;
      }

      ::v-deep {
        .elx-table--header-wrapper {
          background-color: rgb(233, 243, 255);
        }

        .elx-table {
          font-size: 0.14rem !important;
        }

        .elx-body--column {
          height: 0.48rem !important;
          line-height: 0.24rem;
        }

        .elx-header--column {
          background-image: none !important;
          height: 0.48rem !important;
        }

        .el-date-editor.el-input,
        .el-date-editor.el-input--inner {
          width: 100% !important;
        }

        .el-form-item--small.el-form-item {
          margin-bottom: 0px;
          margin-top: 18px;
        }
      }

      .submit-button {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>
